<?php

class Products_controller extends Application_controller
{

	function index() {
		$this->list_items();
		$this->render( 'products' , 'list_items' );
	}

	function list_items() {
		$this->products = Product::find('all', 'Product');
	}

	function show() {
		$this->product = Product::find(ID, 'Product');
	}

	function new_item() {
		$this->product = new Product();
	}

	function create() {
		$product = new Product($_POST['product']);
		if ($product->save()) {
			$this->setnotice('Product saved successfully!');
			$this->redirect('products', 'list_items');
		}
		else {
			$this->product = $product;
			$this->setnotice('Product NOT SAVED!');
			$this->render('products', 'new_item');
		}
		
	}

	function edit() {
		$this->product = Product::find(ID, 'Product');
	}

	function update() {
		$product = Product::find(ID, 'Product');
		if ($product->update($_POST['product'])) {
			$this->setnotice('Product updated successfully!');
			$this->redirect('products', 'list_items');
		}
		else {
			$this->product = $product;
			$this->setnotice('Product NOT updated!');
			$this->render('products', 'edit');
		}
		
	}

	function destroy() {
		$product = Product::find(ID, 'Product');
		$product->destroy();
		$this->setnotice('Product has been successfully removed!');
		$this->redirect('products', 'list_items');
	}

}

?>